package com.example.market;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Build;
import android.os.Bundle;
import android.os.StrictMode;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.Toast;

import org.json.JSONException;
import org.json.JSONObject;

import java.io.IOException;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.MediaType;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;

public class MainActivity extends AppCompatActivity {
FrameLayout flv;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main2);


if (Build.VERSION.SDK_INT>9){
    StrictMode.ThreadPolicy policy=new StrictMode.ThreadPolicy.Builder().permitAll().build();
    StrictMode.setThreadPolicy(policy);
}


     flv=findViewById(R.id.flv);
        Login();

        try {
            Thread.sleep(3000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }


        FragmentRemen f3=new FragmentRemen();
     getSupportFragmentManager().beginTransaction().replace(R.id.flv,f3).commit();



    }




    private void Login() {


        String url = "http://124.93.196.45:10001/dev-api/login";
        OkHttpClient client = new OkHttpClient();
        JSONObject object = new JSONObject();
        try {
            object.put("username", "16607699001");
            object.put("password", "123456");

        } catch (JSONException e) {
            e.printStackTrace();
        }


        MediaType mediaType = MediaType.parse("application/json");
        RequestBody body = RequestBody.create(mediaType, String.valueOf(object));


        okhttp3.Request request = new Request.Builder().url(url).post(body).build();

        Call call = client.newCall(request);

        call.enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {


                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {

                        Toast.makeText(MainActivity.this, "login失败。。。。。。。。。。。。", Toast.LENGTH_SHORT).show();

                    }
                });




            }


            public void onResponse(Call call, Response response) throws IOException {

                String json = response.body().string();
                JSONObject object1 = null;
                try {
                    object1 = new JSONObject(json);
                    Myapp.token = object1.getString("token");



                } catch (JSONException e) {
                    e.printStackTrace();
                }



            }
        });

    }


          public void ddd(View view){

                 FragmentSearch f1=new FragmentSearch();
                 FragmentUpdate f2=new FragmentUpdate();
                 FragmentRemen f3=new FragmentRemen();

                 switch (view.getId()){
                     case R.id.remeng:
                         getSupportFragmentManager().beginTransaction().replace(R.id.flv,f3).commit();
                         break;
                     case R.id.upmenu:
                         getSupportFragmentManager().beginTransaction().replace(R.id.flv,f2).commit();
                         break;
                     case R.id.sosomenu:
                         getSupportFragmentManager().beginTransaction().replace(R.id.flv,f1).commit();
                         break;

                 }


          }

}